
function cko_inventory()
  r = {}
  for k,v in cko_inventory_map do
    r[v] = 0
  end
  r.empty = 0

  for bag=0,4 do
    for slot=1,GetContainerNumSlots(bag) do
      x1 = GetContainerItemLink(bag,slot)
      if x1 then
        _,n = GetContainerItemInfo(bag,slot)
        for k,v in cko_inventory_map do
          if string.find(x1,k) then
            r[v] = r[v] + n
          end
        end
      else
        r.empty = r.empty + 1
      end
    end
  end
  r.ammo = GetInventoryItemCount("player",GetInventorySlotInfo("AmmoSlot"))
  return r
end

function cko_inventory_check()
  local u = UnitName("player")
  if u=='Elluvianna' then
    cko_inventory_check_elluvianna()
  end
  if u=='Elfinaria' then
    cko_inventory_check_elfinaria()
  end
  if u=='Antic' then
    cko_inventory_check_antic()
  end
end

function cko_inventory_check_antic()
  local x = cko_inventory()
  if r.ammo < 900 then
    cko_print("*** LOW: Ammo = ".. x.ammo)
  end
  cko_print("Empty slots = ".. x.empty)
end

function cko_inventory_check_elfinaria()
  local x = cko_inventory()
  if x.water < 50 then
    cko_print("*** LOW: Water = ".. x.water)
    CastSpellByName("Conjure Water")
  end
  if x.food < 50 then
    cko_print("*** LOW: Food = ".. x.food)
    CastSpellByName("Conjure Food")
  end
  if x.mana_stone < 1 then
    cko_print("*** LOW: Mana stone = ".. x.mana_stone)
    CastSpellByName("Conjure Mana Jade")
  end
  if x.healing_potion<10 then
    cko_print("*** LOW: Healing potion = ".. x.healing_potion)
  end
  if x.trolls_blood<10 then
    cko_print("*** LOW: Troll's blood = ".. x.trolls_blood)
  end
  cko_print("Empty slots = ".. x.empty)
end

function cko_inventory_check_elluvianna()
  local x = cko_inventory()
  if r.ammo < 200 then
    cko_print("*** LOW: Ammo = ".. x.ammo)
  end
  if r.flash_powder<20 then
    cko_print("*** LOW: Flash Powder = ".. x.flash_powder)
  end
  if r.instant_poison<20 then
    cko_print("*** LOW: Instant Poison = ".. x.instant_poison)
  end
  if r.crippling_poison<20 then
    cko_print("*** LOW: Crippling Poison = ".. x.crippling_poison)
  end
  cko_print("Empty slots = ".. x.empty)
end

function cko_inventory_macro()
  cko_macro_create('inventory',0,'/script cko_inventory_check()')
  cko_macro_create('bandage',0,'/script TargetUnit("player");UseContainerItem(0,1)')
end

